<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: CardInfoController.php
// +----------------------------------------------------------------------
// | Author: liuju
// +----------------------------------------------------------------------
// | Description:  银行卡信息
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019-01-04 Author: liuju Modification: 创建
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Frontend\V10\BankCard;


use App\Http\Controllers\Controller;
use App\Models\AdminBankIcon;
use App\Models\RepayRecord;
use App\Models\RepaySummary;
use App\Models\User;
use App\Models\UserBankCard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class CardInfoController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth:api');
    }


    /**
     * Description: 获取用户银行卡信息
     * Author: liuju
     * DateTime: 2019-01-06 14:00
     * @return mixed
     */
    public function getCardList($cardType='',$loan='')
    {
        //$cardType = 信用卡位1储蓄卡位2，不传值获取全部
        //$loan = 进件1其他不传值

        //如果$cardType不为空
        //则取 $cardType = \Illuminate\Support\Facades\Request::get('card_type');
        //否则 $cardType = ''
        !$cardType && $cardType = \Illuminate\Support\Facades\Request::get('card_type');


        !$loan  && $loan = \Illuminate\Support\Facades\Request::get('loan');

        Log::debug('获取银行卡信息:银行类型->'.$cardType.'->loan是->'.$loan);
        $user = auth('api')->user();


        //取这个人的银行卡 列表
        $cardList = UserBankCard::where(function($query) use ($user){
            return  $query->where('uid',$user->id)->orWhere('phone_num',$user->phone_num);
        })->valid()
            ->select('user_bank_cards.id', 'user_bank_cards.card_num', 'user_bank_cards.bank_name', 'user_bank_cards.bank_name_show', 'user_bank_cards.is_credit')
            ->orderByDesc('user_bank_cards.is_credit')->orderByDesc('user_bank_cards.id');
        if ($cardType && in_array($cardType, array_keys(UserBankCard::CARD_TYPE_TEXT))) {
            $cardList = $cardList->where('is_credit', intval($cardType));
        }


        if($loan){ //只走进件的入口 还款不走

            $cardList->where([
                ['order_num','<>',''],
                ['hold_agreement_num','<>',''],
            ])->whereNull('old_kskd_user_id');

        }




        $cardList = $cardList->get()->toArray();

        if(!$cardList){
            return respond(['card_list' => []], '请求成功');
        }
        //取银行卡图标
        $otherIcon = AdminBankIcon::where('bank_name','其他')->orderByDesc('id')->value('icon_oss_url');
        $otherIcon = Storage::disk('oss')->signUrl($otherIcon);
        $bankIcons = AdminBankIcon::whereIn('bank_name',array_pluck($cardList,'bank_name'))->orWhere('bank_name',array_pluck($cardList,'bank_name_show'))->get([
            'single_limit','day_limit','icon_oss_url','bank_name'
        ])->toArray();
        foreach($bankIcons as $key=>$item) {
            $bankIcons[$item['bank_name']] = $item;
        }
        foreach ($cardList as $key => $item) {
            //格式化银行卡信息
            $data = UserBankCard::appFormat($item);
            if (!empty($data['is_credit'])) {
                unset($data['is_credit']);
            }
            $cardList[$key] = $data;
            if(!$cardList[$key]['bank_name_show']){
                $cardList[$key]['bank_name_show'] = $item['bank_name'];
            }

            //没有图标就选默认
            if( !empty(array_key_exists($item['bank_name'],$bankIcons)) ){
                if(!empty($bankIcons) && array_key_exists('icon_oss_url', $bankIcons[$item['bank_name']])){
                    $cardList[$key]['bank_icon'] = !empty($bankIcons[$item['bank_name']]['icon_oss_url'])
                        ? Storage::disk('oss')->signUrl($bankIcons[$item['bank_name']]['icon_oss_url'])
                        : $otherIcon;
                }else{
                    $cardList[$key]['bank_icon'] =  $otherIcon;
                }
            }else{
                $cardList[$key]['bank_icon'] =  $otherIcon;
            }

            if( !empty(array_key_exists($item['bank_name'],$bankIcons)) ) {
                if (!$bankIcons) {
                    $cardList[$key]['single_limit'] = '';
                } else {
                    !empty(intval($bankIcons[$item['bank_name']]['single_limit'])) ? $cardList[$key]['single_limit'] = '单笔限额'
                        . $bankIcons[$item['bank_name']]['single_limit'] . '元' : $cardList[$key]['single_limit'] = '';
                }
            }else{
                $cardList[$key]['single_limit'] = '';
            }

            if( !empty(array_key_exists($item['bank_name'],$bankIcons)) ) {
                if (!$bankIcons) {
                    $cardList[$key]['day_limit'] = '';
                } else {
                    !empty(intval($bankIcons[$item['bank_name']]['day_limit'])) ? $cardList[$key]['day_limit'] = '单日限额'
                        . $bankIcons[$item['bank_name']]['day_limit'] . '元' : $cardList[$key]['day_limit'] = '';
                }
            }else{
                $cardList[$key]['day_limit'] = '';
            }

            $cardList[$key]['is_checked'] = ($item['id'] == $user->using_bank_card) ? 1:0;
        }
        return respond([
            'card_list' => $cardList,
        ], '请求成功');
    }


    /**
     * Description: 通过id获取用户卡号
     * Author: liuju
     * DateTime: 2019-01-04 12:08
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCardNum($id)
    {
        $card = UserBankCard::find($id);
        if (!$card) {
            return failed('请求错误，银行卡不存在');
        }
        if ($card->uid != auth('api')->user()->id) {
            return failed('请求失败，您无权获取该银行卡信息');
        }
        return respond([
            'card_num' =>$card->card_num ,
        ]);
    }

    /**
     * Description: 通过银行卡号获取用户协议号
     * Author: liuju
     * DateTime: 2019-01-09 11:27
     * @param $id
     * @return mixed
     */
    public function  getHoldAgreementNum($id)
    {
        $card = UserBankCard::find($id);
        if (!$card) {
            return failed('请求错误，银行卡不存在');
        }
        if ($card->uid != auth('api')->user()->id) {
            return failed('请求失败，您无权获取该银行卡信息');
        }
        return respond([
            'hold_agreement_num' =>$card->hold_agreement_num ,
        ]);
    }
}